package me.jessyan.armscomponent.app.mvp.presenter.user;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.armscomponent.commonsdk.http.BaseResponse;
import me.jessyan.armscomponent.app.mvp.contract.user.UserSetUserinfoContract;
import me.jessyan.armscomponent.app.mvp.model.entity.RegisterRequest;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


/**
 * <p>
 * Author: Taozebi
 * Date: 08/19/2021 23:21
 * Description: 设置用户信息
 */
@ActivityScope
public class UserSetUserinfoPresenter extends BasePresenter<UserSetUserinfoContract.Model, UserSetUserinfoContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public UserSetUserinfoPresenter(UserSetUserinfoContract.Model model, UserSetUserinfoContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void register() {
        RegisterRequest registerRequest = new RegisterRequest();
        registerRequest.setMobile(mRootView.getUserName());
        registerRequest.setLoginPwd(mRootView.getPassword());
        registerRequest.setPayPwd(mRootView.getPayPassword());
        registerRequest.setVerifyCode(mRootView.getSmsCaptcha());
        registerRequest.setInviteCode(mRootView.getInviteCode());
        registerRequest.setNickname(mRootView.getRealName());
        registerRequest.setGender(mRootView.getGender());
        registerRequest.setCountryCode(mRootView.getCountryCode());

        //短信验证码
//        registerRequest.setVerifyCode("");


        mModel.register(registerRequest)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();

                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();//隐藏下拉刷新的进度条
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<BaseResponse<Object>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseResponse<Object> obj) {
                        if(obj.success()){
                            mRootView.showMessage("用户注册成功");
                            mRootView.registerSuccess(null);
                        }else {
                            BaseResponse.Errmsg errmsg = obj.getErrmsg();
                            if(null != errmsg){
                                mRootView.showMessage(errmsg.getEn());
                            }else{
                                mRootView.showMessage("接口返回参数异常");
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable t) {
                        super.onError(t);
                        mRootView.showMessage("用户注册失败,"+t.getMessage());
                    }
                });
    }

}
